package materialdesign.zjitc.net.materialdesign;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private ViewPager mVp;
    private List<Fragment> mFragments;
    private List<String> mTitles;

    /**
     * 步骤：
     * 1.添加依赖
     * 2.修改style NoActionBar
     * 3.定义主布局activity_main.xml
     * 4.资源文件下创建menu包，创建mian.xml(Toolbar)
     * 5.创建fragment所对应的Activity，fragment.xml(Tablayout)
     * 6.创建FragmentAdapter，继承 FragmentPagerAdapter(TabLayout)
     * 7.创建RecyclerAdapter，继承RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>（RecyclerView）
     * 8.创建fragment对应的ListFragment对象（Tablayout）
     * 9.定义MainActivity
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initViews();
    }

    private void initViews() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.share:
                        Toast.makeText(MainActivity.this, "分享", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.setting:
                        Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
        mTitles.add("动漫");
        mTitles.add("动漫");
        mTitles.add("动漫");
        for (int i = 0; i < mTitles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTitles.get(i)));
        }

        List<String> mContent = new ArrayList<>();
        mContent.add("动漫");
        mContent.add("动漫");
        mContent.add("动漫");
        mContent.add("动漫");
        mContent.add("动漫");
        mContent.add("动漫");
        mContent.add("动漫");
        mContent.add("动漫");
        mContent.add("动漫");

        ListFragment1 listFragment1 = new ListFragment1(mContent);
        ListFragment2 listFragment2 = new ListFragment2();
        ListFragment3 listFragment3 = new ListFragment3();
        mFragments.add(listFragment1);
        mFragments.add(listFragment2);
        mFragments.add(listFragment3);
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentAdapter(getSupportFragmentManager(), mTitles, mFragments);
        mTabLayout.setupWithViewPager(mVp);
        mVp.setAdapter(fragmentPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void init() {
        mToolbar = findViewById(R.id.toolbar);
        mTabLayout = findViewById(R.id.tablayout);
        mVp = findViewById(R.id.viewpager);
        mFragments = new ArrayList<>();
        mTitles = new ArrayList<>();
    }
}
